/* Archive Extractor
 * Copyright 2008-2009 Matthew Hoops
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $URL: http://archive-extractor.googlecode.com/svn/trunk/file_reader.cpp $
 * $Id: file_reader.cpp 2 2009-03-23 01:29:43Z clone2727 $
 *
 */

#include "file_reader.h"

byte readByte(FILE *handle) {
	byte b;
	fread(&b, 1, 1, handle);
	return b;
}

int8 readSint8(FILE *handle) {
	return (int8)readByte(handle);
}

uint16 readUint16LE(FILE *handle) {
	return readByte(handle) + (readByte(handle) << 8);
}

int16 readSint16LE(FILE *handle) {
	return (int16)readUint16LE(handle);
}

uint16 readUint16BE(FILE *handle) {
	return (readByte(handle) << 8) + readByte(handle);
}

int16 readSint16BE(FILE *handle) {
	return (int16)readUint16BE(handle);
}

uint32 readUint32LE(FILE *handle) {
	return readUint16LE(handle) + (readUint16LE(handle) << 16);
}

int32 readSint32LE(FILE *handle) {
	return (int32)readUint32LE(handle);
}

uint32 readUint32BE(FILE *handle) {
	return (readUint16BE(handle) << 16) + readUint16BE(handle);
}

int32 readSint32BE(FILE *handle) {
	return (int32)readUint32BE(handle);
}

uint32 getFileSize(FILE *handle) {
	uint32 oldPos = ftell(handle);
	fseek(handle, 0, SEEK_END);
	uint32 size = ftell(handle);
	fseek(handle, oldPos, SEEK_SET);
	return size;
}
